package com.wanhe.viewconf;

import com.orhanobut.logger.Logger;
import com.wanhe.base.model.disgn.Event;
import com.wanhe.base.model.disgn.ViewEvent;
import com.wanhe.viewconf.views.child.BaseXYView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * File Name:EventManager.java<br/>
 * Package Name:com.wanhe.viewconf<br/>
 * Date:2017/7/11 14:02<br/>
 * author:chendeji<br/>
 * desc: 通过处理事件中的方法，来进行java反射，反射出子控件的方法然后执行<br/>
 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
 */
public class EventManager {
	public static final String	TAG	= "EventManager";
	private static EventManager	ourInstance;

	public static EventManager getInstance() {
		if (ourInstance == null) {
			synchronized (EventManager.class) {
				if (ourInstance == null) {
					ourInstance = new EventManager();
				}
			}
		}
		return ourInstance;
	}

	private EventManager() {
	}

	/**
	 * Methor Name:onReceiveEvent<br/>
	 * Package Name:com.wanhe.viewconf<br/>
	 * Date:2017/7/11 14:05<br/>
	 * author:chendeji<br/>
	 * desc: 接受到控件和数据之后，对数据中的执行方法进行提取，然后执行相应的子控件方法<br/>
	 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
	 *
	 * @param view
	 *            接受到时间的子控件
	 * @param event
	 *            从别的子控件传递过来的事件
	 * @param coor
	 *            发送事件的控件的具体位置信息和大小信息
	 * @return
	 */
	public void onReceiveEvent(BaseXYView view, Event event, ViewEvent.Coor coor) {
		onReceiveEvent(view, event.getCop(), event, coor);
	}

	public void onReceiveEvent(BaseXYView view, String eventName, Event event, ViewEvent.Coor coor){
		/**
		 * 使用反射的方式将事件中提取到的执行方法，将子控件中的方法反射出来执行，并传入执行的方法参数
		 *
		 * @author 陈德基
		 * @time 2017/7/11 14:06
		 */
		Class<? extends BaseXYView> clazz = view.getClass();
		try {
			Method declaredMethod = clazz.getDeclaredMethod(eventName, Event.class,
					ViewEvent.Coor.class);
			declaredMethod.invoke(view, event, coor);
		} catch (Exception e) {
			Logger.t(TAG).e(e.getMessage());
		}
	}
}
